package com.xinruke.hostels.central.manage.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xinruke.hostels.central.manage.interfaces.dto.sensitiveword.*;
import com.xinruke.hostels.central.manage.mapper.XtMinganciMapper;
import com.xinruke.hostels.common.cache.ManageUserCache;
import com.xinruke.hostels.common.cache.vo.LoginManageResultVO;
import com.xinruke.hostels.common.constants.SysDicConstant;
import com.xinruke.hostels.common.mybatis.MyExample;
import com.xinruke.hostels.common.po.XtMinganci;
import com.xinruke.hostels.common.po.constants.XtMinganciConstants;
import com.xinruke.hostels.common.vo.ResultVO;
import com.xinruke.hostels.common.vo.query.EConditionOperator;
import com.xinruke.hostels.common.vo.query.QueryConditions;
import com.xinruke.hostels.common.vo.query.RowsDataVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author yutaipu
 */
@Service
public class SensitiveWordService {

    @Autowired
    private XtMinganciMapper xtMinganciMapper;

    @Autowired
    private ManageUserCache manageUserCache;

    @Transactional(rollbackFor = Exception.class)
    public ResultVO add(SensitiveWordAddDTO params) {
        ResultVO resultVO = ResultVO.success();

        LoginManageResultVO user = manageUserCache.getLoginManageUserInfo();

        XtMinganci add = new XtMinganci();
        BeanUtils.copyProperties(params, add);
        add.setSuoshuxitong(XtMinganciConstants.SUOSHUXITONG_ZHONGYANGXITONG);

        add.setChuangjianshijian(new Date());
        add.setChuangjianrenId(user.getId());
        add.setGengxinrenId(user.getId());
        add.setGengxinshijian(new Date());
        add.setShanchubiaoshi(SysDicConstant.DELFLAG_0);
        int insert = xtMinganciMapper.insertSelective(add);
        if (insert == 0) {
            return ResultVO.fail();
        }
        return resultVO;
    }

    @Transactional(rollbackFor = Exception.class)
    public ResultVO modify(SensitiveWordModifyDTO params) {
        ResultVO resultVO = ResultVO.success();

        XtMinganci modify = new XtMinganci();
        BeanUtils.copyProperties(params, modify);
        modify.setGengxinrenId(manageUserCache.getCurrentUserId());
        modify.setGengxinshijian(new Date());
        int count = xtMinganciMapper.updateByPrimaryKeySelective(modify);
        if (count == 0) {
            return ResultVO.fail();
        }
        return resultVO;
    }

    @Transactional(rollbackFor = Exception.class)
    public ResultVO delete(SensitiveWordDeleteDTO params) {
        ResultVO resultVO = ResultVO.success();

        XtMinganci delete = new XtMinganci();
        BeanUtils.copyProperties(params, delete);
        delete.setGengxinshijian(new Date());
        delete.setGengxinrenId(manageUserCache.getCurrentUserId());
        delete.setShanchubiaoshi(SysDicConstant.DELFLAG_1);
        int insert = xtMinganciMapper.updateByPrimaryKeySelective(delete);
        if (insert == 0) {
            return ResultVO.fail();
        }
        return resultVO;
    }

    public ResultVO<RowsDataVO<SensitiveWordQueryResultDTO>> list(SensitiveWordQueryListDTO params) {

        RowsDataVO<SensitiveWordQueryResultDTO> dataVO = new RowsDataVO();
        QueryConditions conditions = new QueryConditions();

        conditions.addCondition("shanchubiaoshi", EConditionOperator.EQULS, SysDicConstant.DELFLAG_0);
        conditions.addCondition("guanjianci", EConditionOperator.LIKE, params.getGuanjianci());
        conditions.addCondition("pingbijibie", EConditionOperator.EQULS, params.getPingbijibie());

        MyExample example = new MyExample(XtMinganci.class, conditions, params.getOrderFields());
        Page<XtMinganci> page = PageHelper.startPage(params.getPageNo(), params.getPageSize());
        List<XtMinganci> list = xtMinganciMapper.selectByExample(example);

        List<SensitiveWordQueryResultDTO> dtoList = list.stream().map(n -> {
            SensitiveWordQueryResultDTO dto = new SensitiveWordQueryResultDTO();
            BeanUtils.copyProperties(n, dto);
            return dto;
        }).collect(Collectors.toList());

        dataVO.setPageNo(page.getPageNum());
        dataVO.setTotalRecords(page.getTotal());
        dataVO.setTotalPages(page.getPages());
        dataVO.setRows(dtoList);
        return ResultVO.success("查询成功", dataVO);
    }

    public ResultVO<SensitiveWordQueryResultDTO> detail(SensitiveWordDetailDTO params) {

        SensitiveWordQueryResultDTO dto = new SensitiveWordQueryResultDTO();
        XtMinganci detail = xtMinganciMapper.selectByPrimaryKey(params.getId());

        if (null == detail) {
            return ResultVO.fail("没有此记录");
        }
        BeanUtils.copyProperties(detail, dto);
        return ResultVO.success("获取详情成功", dto);
    }

    public ResultVO<SensitiveWordCheckResultDTO> check(SensitiveWordCheckDTO params) {
        List<SensitiveWordCheckDetailDTO> list = xtMinganciMapper.listAll(params.getSuoshuxitong());

        String content = params.getLeirong();
        for (SensitiveWordCheckDetailDTO dto : list) {
            if (content.contains(dto.getGuanjianci())) {
                content = content.replace(dto.getGuanjianci(), dto.getTihuanneirong());
            }
        }

        SensitiveWordCheckResultDTO result = new SensitiveWordCheckResultDTO();
        result.setLeirong(content);
        return ResultVO.success("校验完成", result);
    }
}
